Page({
  data: {
    jiage: 0, //总价格
    goods_list: [],

  },

  onLoad: function (options) {
    try {
      var value = wx.getStorageSync('gwc')
      if (value) {
        console.log(value);
        this.setData({
          goods_list: value
        })
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  onReady: function () {
    this.jisuanJiaGe()
  },

  onShow: function () {

  },



  /**
   * 数量的++，和--的方法
   *   @param {*} e 
   */
  numChange(e) {
    console.log(e);
    let {
      index,
      type,
      list
    } = e.currentTarget.dataset, list0 = this.data[list];
    let arr = this.data.goods_list
    list0.forEach((item, index0) => {
      if (index == index0) {
        if (type == 0) {
          if (item.num <= 1) {
            return wx.showModal({
              title: '删除?',
              content: '最后一个了确定要删除',
              success: res => {
                if (res.confirm) {
                  arr.splice(index, 1)
                  this.setData({
                    goods_list: arr
                  })
                  this.jisuanJiaGe()
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
          item.num--;
        } else {
          if (index == index0) {
            if (item.num >= 10) {
              return wx.showToast({
                title: '不能更多了哦',
                icon: 'none'
              })
            }
            item.num++;
          }
        }
      }
    })
    this.jisuanJiaGe()
    this.setData({
      [list]: list0
    })
  },

  //计算价格
  jisuanJiaGe() {
    let arr = this.data.goods_list;
    console.log(arr);
    let int = 0;
    for (let i = 0; i < arr.length; i++) {
      int += arr[i].num * arr[i].price
    }
    this.setData({
      jiage: int,
    })
  },


  //确认购买
  querengoumai() {
    wx.showLoading({
      title: '正在提交',
      mask: true
    })
    let arr = this.data.goods_list;
    let zongjiage = this.data.jiage;
    var value = wx.getStorageSync('autt')
    if (value.jf >= zongjiage) {
      let dataobj = {
        name: value.aut,
        id: value.id,
        jf: value.jf,
        daarr: arr,
        zongjiage: this.data.jiage,
      }
      wx.cloud.callFunction({
          // 云函数名称
          name: 'goumai',
          // 传给云函数的参数
          data: dataobj,
          success: (res) => {
            this.tiaozhuan(res);
          }
        })
    } else {
      setTimeout(function () {
        wx.hideLoading()
      }, 1500)
      wx.showModal({
        title: '提示',
        content: '积分不够呢',
        success: res => {
          if (res.confirm) {
            arr.splice(index, 1)
            this.setData({
              goods_list: arr
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  tiaozhuan(res) {
    if (res.result.msg) {
      wx.removeStorage({
        key: 'gwc',
        success(res) {
          console.log(res)
        }
      })
      console.log("zhixingl");
      let stee = '/pages/shangdian/cardVoucher/index'
      wx.redirectTo({
        url: stee,
      });
    } else {
      var usssss = wx.getStorageSync('autt')
      usssss.user = res.result.data,
        usssss.jf = res.result.data.jf
      wx.setStorage({
        key: 'autt',
        data: usssss
      })
      wx.showLoading({
        title: '可能积分不够',
        mask: true
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 1500)
    }
  }

})